package apptoko.com;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import apptoko.com.fragment.MenuFragment;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.util.AQUtility;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.SlidingMenu.OnClosedListener;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class BaseActivity extends SlidingFragmentActivity implements
		OnClosedListener {

	private int mTitleRes;
	protected ListFragment mFrag;
    private SlidingMenu sm;
    public Menu menu;
    private String language="";
	public BaseActivity(int titleRes) {
		mTitleRes = titleRes;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		language= UILApplication.getInstance().getConfig().locale.getLanguage();
	}

	private void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.list_pager);
		setTitle(mTitleRes);
		EasyTracker.getInstance().setContext(this);
		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
		mFrag = new MenuFragment();
		t.replace(R.id.menu_frame, mFrag);
		t.commit();
		
		// customize the SlidingMenu
		sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		sm.setOnClosedListener(this);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@SuppressLint("NewApi")
	@Override
	protected void onResume() {
		Log.e("onResume","onResumeBase");
		if(language.equals(UILApplication.getInstance().getConfig().locale.getLanguage())){
			super.onResume();
		}else{
			super.onResume();
			initView();
			language= UILApplication.getInstance().getConfig().locale.getLanguage();
		}
		invalidateOptionsMenu();
		com.facebook.AppEventsLogger.activateApp(this, getResources().getString(R.string.fb_app_id));
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		case R.id.menu_search:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "search-button", Long.valueOf(10));
			Intent search = new Intent(this, SearchActivity.class);
			search.putExtra(Constants.SEARCH_TYPE, "all");
			startActivity(search);
			return true;
		case R.id.menu_account:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "menu-account", Long.valueOf(1));
			boolean isLogin = Utils.getStaticIsLogin(this);
			if (isLogin) {
				Intent account = new Intent(BaseActivity.this,
						AccountActivity.class);
				startActivity(account);
			} else {
				Intent login = new Intent(BaseActivity.this,
						LoginActivity.class);
				startActivity(login);
			}
			return true;
		case R.id.menu_manage:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "menu-manage", Long.valueOf(1));
			Intent i = new Intent(BaseActivity.this, AppManageActivity.class);
			i.putExtra(Constants.STORE_KEY, "apps");
			startActivity(i);
			return true;
		case R.id.menu_setting:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "menu-setting", Long.valueOf(1));
			Intent setting = new Intent(BaseActivity.this, SettingsActivity.class);
			startActivityForResult(setting, 1150);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_home, menu);
//		this.menu = menu;
		return true;
	}
	
	@Override
	public void onClosed() {
		// TODO Auto-generated method stub
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(isTaskRoot()){
            //clean the file cache with advance option
            long triggerSize = 10000000; //starts cleaning when cache size is larger than 10M
            long targetSize = 2000000;      //remove the least recently used files until cache size is less than 2M
            AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
		}
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.e("", "prepare "+getResources().getString(R.string.account));
		
	menu.findItem(R.id.menu_account)
		.setTitle(getResources().getString(R.string.account));
	menu.findItem(R.id.menu_manage)
		.setTitle(getResources().getString(R.string.manage));
	menu.findItem(R.id.menu_setting)
		.setTitle(getResources().getString(R.string.setting));
		Log.e("", "prepared "+menu.findItem(R.id.menu_account).getTitle());
		
		return true;
	}
}
